package com.zte.shopping.action.frontend;

import com.zte.shopping.constant.DictConstant;
import com.zte.shopping.entity.Order;
import com.zte.shopping.entity.User;
import com.zte.shopping.exception.UserNotLoginException;
import com.zte.shopping.service.OrderService;
import com.zte.shopping.util.ParameterUtil;
import com.zte.shopping.vo.CartVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: ssm
 * @description: 前台订单管理
 * @author: zuti
 * @create: 2020-06-30 09:49
 **/
@Controller
@RequestMapping("order")
public class FrontOrderController {


    private  static  final Logger logger= Logger.getLogger(FrontOrderController.class);

    @Autowired
    OrderService orderService;


    /**
     * @Author zuti
     * @Description
     * //TODO zuti666
     * 生成订单信息 以及  该订单名下的所有明细信息
     * 添加一个订单
     * 添加多个明细
     * @Date 21:20 2020/6/30
     * @Param [session]
     * @return java.lang.String
     **/

    @ResponseBody
    @RequestMapping("/createOrder")
    public String createOrder(HttpSession session)
    {
        CartVo cart = (CartVo)session.getAttribute("cart");
        User user = (User)session.getAttribute("user");

        String no = orderService.createOrder(user, cart);

        session.setAttribute("cart", new CartVo());

        return no;
    }

    /**

     */
    /**
     * @Author zuti
     * @Description //TODO * "我的订单" 查询该会员下的订单信xi
     * @Date 21:19 2020/6/30
     * @Param [pageNo, pageSize, session]
     * @return org.springframework.web.servlet.ModelAndView
     **/

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo, String pageSize, HttpSession session) throws UserNotLoginException {
        ModelAndView  modelAndView = new ModelAndView();

        User user = (User) session.getAttribute("user");

        if(user==null) {
            logger.debug("FrontOrderController====>findAll user为空");

        }else {
            logger.debug("FrontOrderController====>findAll useId="+user.getUserId());
            // 查询某个会员 下的所有订单
            List<Order> orderList = orderService.findByUserId(user);
            modelAndView.addObject("OrderList", orderList);
        }
// 跳转到/WEB-INF/pages/myOrders.jsp 页面
        modelAndView.setViewName("frontend/myOrders");

        return modelAndView;
    }

    /**
     * 根据订单号  查询订单信息
     */
    @RequestMapping("/findDetail")
    public ModelAndView findDetail(String orderId)
    {
        ModelAndView modelandview = new ModelAndView();

        // 根据订单id  查询该订单的详情
        Order order = orderService.findByOrderId(orderId);

        modelandview.addObject("order", order);

        modelandview.setViewName("frontend/orderDetail");

        return modelandview;
    }


}
